$(function() {
    $("#optionTable").bootstrapTable('destroy').bootstrapTable({
        url : window.portAddress + "/option/optionList",
        dataType : "json",
        method : 'get',
        striped: true,  //行间隔色
        cache : false,  //禁用缓存
        pageNumber: 1,  //初始化加载第一页
        pagination: true,//是否分页
        sidePagination: "client",//前端分页
        pageSize: 5,//单页记录数
        pageList: [5, 10, 15],//可选择单页记录数
        clickToSelect : true,
        theadClasses: "thead-pink",
        queryParams: function (params) {
            //向服务器传递的参数
            return {
                customerNo : $("#customerNo").val(),
                putCcy : $("#putCcy").val(),
                callCcy : $("#callCcy").val(),
                expireDate : $("#expireDate").val(),
                status : $("#status").val(),

            };
        },
        responseHandler : function(res) {
            return res.data;
        },
        columns : [
            {
                title : "客户号",
                field : "customerNo",
                align : "center",

            },
            {
                title : "交易号",
                field : "optionNumber",
                align : "center",
            },
            {
                title : "买入货币",
                field : "putCcy",
                align : "center",
            },
            {
                title : "买入金额",
                field : "putAmount",
                align : "center",
            },
            {
                title : "卖出货币",
                field : "callCcy",
                align : "center",
            },
            {
                title : "卖出金额",
                field : "callAmount",
                align : "center",
            },
            {
                title : "交易日期",
                field : "tradeDate",
                align : "center",
            },
            {
                title : "交割日期",
                field : "expireDate",
                align : "center",
            },
            {
                title : "标准汇率",
                field : "standardRate",
                align : "center",
            },
            {
                title : "成交汇率",
                field : "strikeRate",
                align : "center",
            },

            {
                title : "状态",
                field : "status",
                align : "center",
            }
            ,
            {
                title : "操作",
                field : "status",
                formatter : operation,
                width : 130,
                height: 40,
                align : "center",
            }

        ]
    });

    function operation(value,row,index) {
        var  htm;
        if(value == 'NOR'){
            return htm = "<span class=\"badge badge-warning\">APPROVED</span>"
        }else if (value == 'REJ') {
            return htm = "<span class=\"badge badge-warning\">REJECTED</span>"
        }else if (value == 'DON'){
                return htm = "<span class=\"badge badge-warning\">SETTLED</span>"
        }else {
            htm = "<button type=\"button\" class=\"btn btn-danger\" style='width: 70px;' data-target=\"#rejectModel\" data-toggle=\"modal\">Reject</span></button> "
                + "<button type=\"button\" class=\"btn btn-primary\" style='width: 70px;' data-target=\"#approveModel\" data-toggle=\"modal\">Confirm</button> ";
        }
        return htm;
    }

    $("#rejectModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var optionNumber = thisBtn.closest('tr').find('td').eq(1).text();
        $("#optionNumber").val(optionNumber);
    });

    $("#approveModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var confirmOptionNumber = thisBtn.closest('tr').find('td').eq(1).text();
        $("#confirmOptionNumber").val(confirmOptionNumber);
    });


    $("#rejectOption").click(function() {
        $.ajax({
            url : window.portAddress + "/option/reject",
            type : "post",
            data : {
                optionNumber : $("#optionNumber").val(),
                userName: window.sessionStorage.getItem("userName"),
                remark : $("#remark").val(),
            },
            dataType : "json",
            success : function(data) {
                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
                $("#optionTable").bootstrapTable("refresh");
                $("#rejectModel").modal('hide');
            }
        })
    });

    $("#confirmOption").click(function() {
        $.ajax({
            url : window.portAddress + "/option/confirm",
            type : "post",
            data : {
                optionNumber : $("#confirmOptionNumber").val(),
                userName: window.sessionStorage.getItem("userName"),
                remark : $("#confirmRemark").val(),
            },
            dataType : "json",
            success : function(data) {
                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
                $("#optionTable").bootstrapTable("refresh");
                $("#approveModel").modal('hide');
            }
        })
    });

    $("#search").click(function() {
        $.ajax({
            url : window.portAddress + "/option/optionList",
            type : "post",
            data : {
                customerNo : $("#customerNo").val(),
                putCcy : $("#putCcy").val(),
                callCcy : $("#callCcy").val(),
                expireDate : $("#expireDate").val(),
                status : $("#status").val(),
            },
            dataType : "json",
            success : function(data) {
                if (data.state === '000000') {
                    toastr.success("查询数据成功");
                    $("#optionTable").bootstrapTable("refresh");
                }else {
                    toastr.error(data.message);
                }

            }
        })
    });


})
